/**
 * Notes: 活动后台管理模块业务逻辑
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY cclinux0730 (wechat)
 * Date: 2022-06-24 07:48:00 
 */

const BaseBiz = require('../../../comm/biz/base_biz.js');
const ActivityBiz = require('./activity_biz.js');
const projectSetting = require('../public/project_setting.js');

class AdminActivityBiz extends BaseBiz {
	static async initFormData(id = '') {
		let cateIdOptions = await ActivityBiz.getCateList();

		return {
			id,
			cateIdOptions,
			fields: projectSetting.ACTIVITY_FIELDS,
			formCateId: 1,

			formTitle: '我想在球场见到你，不论风雨',

			formMaxCnt: 12,
			formStart: this.getStartTime(),
			formEnd: this.getEndTime(),
			formStop: this.getEndTime(),

			formAddress: '',
			formAddressGeo: {
				address: '',
				latitude: 0,
				longitude: 0,
				name: ''
			},
			formForms: [],
		}

	}

	static selectLocation(that) {
		let callback = function (res) {
			if (!res || !res.name || !res.address || !res.latitude || !res.longitude)
				return;

			let formAddress = res.address + '  ' + res.name;

			let formAddressGeo = {};
			formAddressGeo.name = res.name;
			formAddressGeo.address = res.address;
			formAddressGeo.latitude = res.latitude;
			formAddressGeo.longitude = res.longitude;
			that.setData({
				formAddressGeo,
				formAddress
			});
		}
		if (that.data.formAddressGeo && that.data.formAddressGeo.latitude > 0) {
			wx.chooseLocation({
				latitude: that.data.formAddressGeo.latitude,
				longitude: that.data.formAddressGeo.longitude,
				success: function (res) {
					callback(res);
				}
			})
		} else {
			wx.chooseLocation({
				success: function (res) {
					callback(res);
				},
				fail: function (err) {
					console.log(err);
				}
			})
		}
	}
	
	static getStartTime() {
		let now = new Date(); // 获取当前时间
		let year = now.getFullYear(); // 获取当前年份
		let month = now.getMonth() + 1; // 获取当前月份，加“1”是因为月份是从0开始计算的，但实际月份是从1开始的
		let day = now.getDate() + 2; // 获取当前日期
		return year + '-' + ('00' + month).substr(-2) + '-' + ('00' + day).substr(-2) + ' 19:00';
	}
	
	static getEndTime() {
		let now = new Date(); // 获取当前时间
		let year = now.getFullYear(); // 获取当前年份
		let month = now.getMonth() + 1; // 获取当前月份，加“1”是因为月份是从0开始计算的，但实际月份是从1开始的
		let day = now.getDate() + 2; // 获取当前日期
		return year + '-' + ('00' + month).substr(-2) + '-' + ('00' + day).substr(-2) + ' 21:00';
	}
}

AdminActivityBiz.CHECK_FORM = {
	title: 'formTitle|must|string|min:2|max:50|name=标题',
	cateId: 'formCateId|must|id|name=分类',

	maxCnt: 'formMaxCnt|must|int|name=人数上限',
	startTime: 'formStart|must|string|name=活动开始时间',
	endTime: 'formEnd|must|string|name=活动结束时间',
	stopTime: 'formStop|must|string|name=报名截止时间',

	address: 'formAddress|must|string|name=活动地点',
	addressGeo: 'formAddressGeo|not|object|name=活动地点GEO',

	forms: 'formForms|array',
};

module.exports = AdminActivityBiz;